package com.cssw.fyzb.busi.api.task;

import com.cssw.fyzb.core.model.vo.task.TaskAndInstVo;
import com.cssw.fyzb.busi.service.task.TaskService;
import com.cssw.fyzb.core.model.dodb.RuleInst;

import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 */
@Function(description = "我的任务", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true),
        @Parameter(name = "pageNumber", type = Type.Integer, description = "当前页，从1开始", required = true),
        @Parameter(name = "pageSize", type = Type.Integer, description = "页大小", required = true),
})
@Component
public class ListMyTask implements Action {

    private final static Logger logger = LoggerFactory.getLogger(ListMyTask.class);

    @Autowired
    private TaskService taskService;

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        int pageNumber = request.getInteger("pageNumber");
        int pageSize = request.getInteger("pageSize");
        try {
            Page<RuleInst> ruleInsts = taskService.myTaskList(userId, pageNumber, pageSize);
            List<TaskAndInstVo> list = new ArrayList<>();
            ruleInsts.getList().forEach(r -> {
                TaskAndInstVo taskAndInst = new TaskAndInstVo();
                taskAndInst.setAllDay(r.getAllDay());
                taskAndInst.setAllValue(r.getAllValue());
                taskAndInst.setDayValue(r.getDayValue());
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                taskAndInst.setCreateTime(format.format(r.getCreateTime()));
                taskAndInst.setEffDate(format.format(r.getEffDate()));
                taskAndInst.setExpDate(format.format(r.getExpDate()));
                taskAndInst.setTaskIcon(r.getStr("task_icon"));
                taskAndInst.setInstId(r.getInstId());
                if (r.getLastDate() != null) {
                    taskAndInst.setLastDate(format.format(r.getLastDate()));
                }
                taskAndInst.setPrice(r.getPrice());
                taskAndInst.setSurplusValue(r.getSurplusValue());
                taskAndInst.setTaskId(r.getTaskId());
                taskAndInst.setTaskName(r.getTaskName());
                taskAndInst.setTaskTime(r.getTaskTime());
                taskAndInst.setUserId(r.getUserId());
                //求当前任务已完成进度 100 -（剩余产值 除 总产值）
                taskAndInst.setToComplete(new BigDecimal(100).subtract
                        (r.getSurplusValue().divide(r.getAllValue(), 3, BigDecimal.ROUND_HALF_UP)
                                .multiply(new BigDecimal(100))));
                list.add(taskAndInst);
            });
            return Response.success().put("list", list);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("查询失败");
        }
    }
}
